package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author guozh
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品和口味
     *
     * @param dishDTO 菜品数据
     * @return Result
     */
    @AutoFill(value = OperationType.INSERT)
    @PostMapping()
    private Result<String> saveDishAndFlavor(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品信息{}", dishDTO);
        dishService.insertDishAndFlavor(dishDTO);

        //清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param dishPageQueryDTO 分页查询参数
     * @return Result<Page < Dish>>
     */
    @GetMapping("/page")
    private Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询数据{}", dishPageQueryDTO);
        PageResult page = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 菜品批量删除
     *
     * @param ids id
     * @return Result<String>
     */
    @DeleteMapping()
    private Result<String> deleteDish(@RequestParam List<Long> ids) {
        log.info("删除数据{}", ids);
        dishService.deleteDish(ids);

        //将所有的菜品缓存数据清理，所有dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id id
     * @return DishVO
     */
    @GetMapping("/{id}")
    private Result<DishVO> getById(@PathVariable Long id) {
        log.info("查询id{}", id);
        DishVO dishVO = dishService.getDishAndFlavorById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     *
     * @param dishDTO 菜品信息
     * @return Result
     */
    @PutMapping()
    @AutoFill(value = OperationType.UPDATE)
    private Result<String> update(@RequestBody DishDTO dishDTO) {
        log.info("修改数据{}", dishDTO);
        dishService.update(dishDTO);

        //将所有的菜品缓存数据清理，所有dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 修改状态
     *
     * @param status 状态
     * @param id     id
     * @return Result<String>
     */

    @AutoFill(value = OperationType.UPDATE)
    @PostMapping("status/{status}")
    private Result<String> updateStatus(@PathVariable Integer status, Long id) {
        log.info("修改id{} 状态 {}", id, status);
        dishService.updateStatus(status, id);

        //将所有的菜品缓存数据清理掉，所有dish_开头的key
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 根据分类查询
     *
     * @param categoryId categoryId
     * @return Result<List < Dish>>
     */
    @GetMapping("/list")
    private Result<List<Dish>> findByCategoryId(Long categoryId) {
        log.info("查询分类id{}", categoryId);
        List<Dish> dishes = dishService.findByCategoryId(categoryId);
        return Result.success(dishes);
    }

    /**
     * 清理缓存数据
     *
     * @param pattern 模式
     */
    private void cleanCache(String pattern) {
        Set key = redisTemplate.keys(pattern);
        redisTemplate.delete(key);
    }
}
